// pages/device/device.js
Page({

  data: {
    systemSetting: {},
    appAuthorizeSetting: {},
    deviceInfo: {},
    systemSwindowInfoetting: {},
    AUTH_MAP: {
      authorized: '已授权',
      denied: '拒绝',
      'not determined': '未确定',
    },
    isShow: false,
    currentBrightness: 0.5
  },

  onLoad() {
    console.clear()

    wx.startAccelerometer({
      interval: 'normal'
    })
    wx.onAccelerometerChange(function(res) {
      console.log(res.x, res.y, res.z)
      const slope = Math.round(Math.atan(res.y / res.z) * 180 / Math.PI)
      console.log('slope', slope);
    })

    wx.getScreenBrightness({
      success: ({ value }) => {
        console.log('屏幕亮度:', value)
        this.setData({
          currentBrightness: value
        })
      }
    })
  },

  onReady() {
    const systemSetting = wx.getSystemSetting()
    const appAuthorizeSetting = wx.getAppAuthorizeSetting()
    const deviceInfo = wx.getDeviceInfo()
    const windowInfo = wx.getWindowInfo()


    this.setData({
      systemSetting,
      appAuthorizeSetting,
      deviceInfo,
      windowInfo
    })
    console.log(systemSetting);

    console.log('蓝牙开关:', systemSetting.bluetoothEnabled)
    console.log('地理位置:', systemSetting.locationEnabled);
    console.log('WIFI:', systemSetting.wifiEnabled);
    console.log('麦克风:', appAuthorizeSetting.microphoneAuthorized);
    console.log('内存大小:', deviceInfo.memorySize);
    console.log('屏幕宽度:', windowInfo.screenWidth);
    console.log('屏幕高度:', windowInfo.screenHeight);


  },

  scanCode() {
    wx.scanCode({
      success: ({ result }) => {
        wx.navigateTo({
          url: `/pages/webview/webview?url=${result}`,
        })
      }
    })
  },
  
  authorize() {
    // scope列表：https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/authorize.html
    wx.getSetting({
      success: ({ authSetting }) => {
        if (!authSetting['scope.record']) {
          wx.authorize({
            scope: 'scope.record',
            success: (res) => {
              console.log(res)
            },
            fail: (err) => {
              wx.openSetting()
              console.log(err);
            }
          })
        }
      }
    })


  },

  onShowSlider() {

    this.setData({
      isShow: true
    })
  },

  onChangeBrightness(e) {
    const value = parseInt(e.detail.value * 10) / 10
    wx.setScreenBrightness({
      value,
      success: () => {
        wx.getScreenBrightness({
          success: ({ value }) => {
            console.log('当前屏幕亮度:', value)
          }
        })
      }
    })
  },

  getSetting() {
    wx.openSetting()
  }



})